package android.slc.appbase.ui.vd;

import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.appbase.ui.vd.wrapper.BaseListWrapper;
import android.slc.appbase.ui.vd.wrapper.IBaseListWrapper;

import androidx.databinding.Observable;

import java.io.Serializable;
import java.util.List;

/**
 * 列表数据基础类
 *
 * @param <T>
 */
public abstract class BaseListVmBox<T extends Serializable> extends BaseListCommonVmBox<T> implements IBaseListWrapper<T> {

    protected final BaseListWrapper<T> listWrapper;

    public BaseListVmBox() {
        this.listWrapper = createListWrapper();
    }

    protected BaseListWrapper<T> createListWrapper() {
        return new BaseListWrapper<>(this);
    }

    @Override
    public void refresh() {
        this.listWrapper.startRefreshOf.notifyChange();
    }

    /**
     * 响应成功
     *
     * @param data
     */
    @Override
    public void onSucceed(List<T> data) {
        this.dataListOf.get().clear();
        this.dataListOf.get().addAll(data);
        this.dataListOf.notifyChange();
    }

    @Override
    protected final void onFailed(int errorCode, String errorMessage) {
        super.onFailed(errorCode, errorMessage);
    }

    @Override
    public void onError(int errorCode, String errorMessage) {
        onFailed(errorCode, errorMessage);
    }

    @Override
    public Observable getStartRefreshOf() {
        return this.listWrapper.getStartRefreshOf();
    }

    @Override
    public SlcObserver<List<T>> getDataListSlcObserver() {
        return this.listWrapper.getDataListSlcObserver();
    }
}
